home *** CD-ROM | disk | FTP | other *** search
- Path: sol.caps.maine.edu!bensh
- From: bensh@gandalf.UMCS.Maine.EDU (Shawn Benn)
- Newsgroups: comp.lang.c
- Subject: Problem with pointers
- Date: 23 Mar 1996 21:50:27 GMT
- Organization: University of Maine, Department of Computer Science
- Distribution: world
- Message-ID: <4j1rn3$19du@sol.caps.maine.edu>
- NNTP-Posting-Host: gandalf.umcs.maine.edu
-
-
- Hi! I have been trying to debug a problem for the past week and have gotten
- absolutely nowhere.
-
- Here's the problem:
-
- I call a function where its supposed to load several nodes into a linked list.
- The linked list has the correct number of elements during the function where
- it initializes and adds all the elements. So I assume this is working correctly.
- The problem exists when I try to access the data in the linked list back in
- the calling function.
-
- Here's the code in question for the function call (ignore the ... as this means
- there are other unimportant (to the problem) parameters to that function):
-
- typedef struct classnode* Class;
-
- Class cls;
- LoadClass(..., &cls, ...);
- printf("%d\n", GetCount(cls));
-
- This printf prints 0.
-
- Here's the code from LoadClass:
-
- void LoadClass(..., Class* cls, ...)
- {
- ...
- for (i=0; i<(int)recs; ++i) {
- ReadStudent(&s, fil, i); /* Read a student and add to the list */
- if (!IsEmpty(*cls)) {
- s1 = GetHead(cls);
- ptr = &cls;
- while ((GetName(&s) > GetName(&s1)) && (ptr->next != NULL)) {
- ptr = ptr->next;
- s1 = ptr->s;
- }
- if ((GetName(&s) > GetName(&s1)) || (ptr->next == NULL))
- AddTail(&cls, s);
- else
- InsertAt(&cls, s, GetIndex(&s1));
- }
- else
- AddHead(&cls, s);
- }
- printf("%d\n", GetCount(&cls));
- }
- }
-
- This printf prints 13 (the actual value for the number of elements in the list
- I am loading). I have also debugged the above routine and went step by step
- through the above loop. Everything seems to be correctly done. I know the
- problem exists in the call to LoadClass, but I do not know the solution. I am
- afraid I do not completely understand how C pointers work.
-
- Thanks for any help.
-
- Shawn Benn
- University of Maine
-